<!--
 * @Author: 黑白灰01 3193273703@qq.com
 * @Date: 2025-01-27 13:57:04
 * @LastEditors: 黑白灰01 3193273703@qq.com
 * @LastEditTime: 2025-02-04 19:35:25
 * @FilePath: /hei_picture_web/src/components/common/GlobalHeader/modal/userInfo.vue
 * @Description: 用户信息模块
-->
<template>
  <div class="user-info">
    <a-dropdown v-if="loginUserStore.loginUserInfo.userId">
      <a-space>
        <a-avatar :src="loginUserStore.loginUserInfo.userAvatar" :size="48" />
        <text style="font-size: 24px">{{ loginUserStore.loginUserInfo.userName }}</text>
      </a-space>
      <template #overlay>
        <a-menu>
          <a-menu-item @click="router.push('/user_info')">
            <a-space>
              <UserOutlined />
              个人资料
            </a-space>
          </a-menu-item>
          <a-menu-item @click="logout">
            <a-space>
              <LogoutOutlined />
              退出登录
            </a-space>
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
    <a-button v-else class="login-btn" type="primary" @click="gotoLogin">
      <UserOutlined />
      登录
    </a-button>
  </div>
</template>

<script setup lang="ts">
import { UserOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { userLogoutUsingPost } from '@/api/userController'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
const loginUserStore = useLoginUserStore()
const router = useRouter()
const logout = async () => {
  const res = await userLogoutUsingPost({})
  if (res.data.code === 200) {
    loginUserStore.setLoginUserInfo({
      userName: '',
      userId: null,
      userAvatar: null,
      userRole: null,
    })
    message.success('退出登录成功')
    sessionStorage.removeItem('loginUserInfo')
    router.push('/')
  }
}
const gotoLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.user-info {
  max-width: 200px;
  display: flex;
  align-items: center;
  margin-right: 20px;
  cursor: pointer;
  text-align: right;
  font-size: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
